<template>
  <div>
    <div class="box">
    <el-form size="mini" :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="工号" prop="id">
        <el-input type="text" v-model.number="ruleForm.id"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名" prop="uname">
        <el-input type="text" v-model="ruleForm.uname"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-input v-model="ruleForm.sex"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="apartment">
        <el-input type="text" v-model="ruleForm.apartment"></el-input>
      </el-form-item>
      <el-form-item label="职位" prop="uposition">
        <el-input type="text" v-model="ruleForm.uposition"></el-input>
      </el-form-item>
      <el-form-item label="月薪" prop="salary">
        <el-input v-model.number="ruleForm.salary"></el-input>
      </el-form-item>
      <el-form-item label="出勤率" prop="attendance">
        <el-input v-model.number="ruleForm.attendance"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
export default {
  name: "AddStaff",
  data() {
    var id = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('工号不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if ((value+"").length!=5) {
              callback(new Error('工号需为5位数'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var uname = (rule, value, callback) => {
        if (!value) {
          callback(new Error('员工姓名不能为空'));
        } else{
          callback()
        }
      };
      var sex = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('性别不能为空'));
        } else{
          callback()
        }
      };
      var apartment = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('部门不能为空'));
        }else{
          callback()
        } 
      };
      var uposition = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('职位不能为空'));
        }else{
          callback()
        } 
      };
      var salary = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('薪资不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          }else{
            callback()
          } 
        }, 1000);
      };
      var attendance = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('出勤率不能为空，新人100%'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          }else{
            callback()
          } 
        }, 1000);
      };
      var email = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('邮箱不能为空'));
        }else{
          callback()
        } 
      };
      var phone = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('手机号不能为空'));
        }else{
          callback()
        } 
      };
      
    return {
      ruleForm: {
          id:'',
          uname:'',
          sex:'',
          apartment:'',
          uposition:'',
          salary:'',
          attendance:'',
          email:'',
          phone:''
        },
        rules: {
          id: [
            { validator: id, trigger: 'blur' }
          ],
          uname: [
            { validator: uname, trigger: 'blur' }
          ],
          sex: [
            { validator: sex, trigger: 'blur' }
          ],
          apartment: [
            { validator: apartment, trigger: 'blur' }
          ],
          uposition: [
            { validator: uposition, trigger: 'blur' }
          ],
          salary: [
            { validator: salary, trigger: 'blur' }
          ],
          attendance: [
            { validator: attendance, trigger: 'blur' }
          ],
          email: [
            { validator: email, trigger: 'blur' }
          ],
          phone: [
            { validator: phone, trigger: 'blur' }
          ]
        }
      
    };
  },
  methods: {
      submitForm(formName) {
        const {id,uname,sex,apartment,uposition,salary,attendance,email,phone}=this.ruleForm
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('addStaff',{id,uname,sex,apartment,uposition,salary,attendance,email,phone})
            alert('添加成功')
          } else {
            alert('添加失败');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
};
</script>

<style lang='less' scoped>
  .box{
    width: 400px;
    margin-left:200px;
  }
</style>